package cn.tedu.dimanage.item.webapi.service;


import cn.tedu.dimanage.common.ex.ServiceException;
import cn.tedu.dimanage.common.web.State;
import cn.tedu.dimanage.item.service.IItemService;
import cn.tedu.dimanage.item.webapi.controller.UploadController;
import cn.tedu.dimanage.item.webapi.mapper.ItemMapper;
import cn.tedu.dimanage.pojo.dto.ItemAddNewDTO;
import cn.tedu.dimanage.pojo.entity.Item;
import cn.tedu.dimanage.pojo.vo.ItemDetailVO;
import cn.tedu.dimanage.pojo.vo.ItemSimpleVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Classname: UserServiceImpl
 * @Author: bromide
 * @CreateTime: 2022/6/10--11:50
 * @Version: V1.0
 * @Description:
 */
@Slf4j
@Service
@DubboService
public class ItemServiceImpl implements IItemService {
    @Autowired
    private ItemMapper mapper;

    @Autowired
    private UploadController uploadController;

    @Override
    public int addItem(ItemAddNewDTO itemAddNewDTO) {
        log.debug("开始处理管理员增加物资的Service");
        // 先判断物资是否存在
        String resultName = itemAddNewDTO.getName();
        if (mapper.getItemDetailsByName(resultName) != null){
            log.debug("物资存在,抛出错误 ");
            throw new ServiceException(State.ERR_ITEM_NAME_DUPLICATE,"添加失败,物资已存在");
        }
        log.debug("物资不存在,补全剩下信息");
        LocalDateTime now = LocalDateTime.now();
        // 创建Item对象
        Item item = new Item();
        // 调用BeanUtils.copyProperties()将参数对象中的属性值复制到Item对象中
        BeanUtils.copyProperties(itemAddNewDTO,item);
        item.setItemCreate(now);
        item.setItemModified(now);
        int row = mapper.addItem(item);
        if (row != 1){
            log.debug("修改之后受影响行数为1,抛出错误");
            throw new ServiceException(State.ERR_INSERT,"添加失败,服务器忙,请稍后再试");
        }
        return row;
    }

    @Override
    public int deleteItemById(Long id) {
        // 数据库删除成功之后删除本地图片文件
        ItemDetailVO itemDetailsById = mapper.getItemDetailsById(id);
        String img = itemDetailsById.getImg();
        log.debug("开始删除商品业务");
        log.debug("删除的图片名为:{}",img);
        int row = mapper.deleteById(id);
        if (row != 1){
            throw new ServiceException(State.ERR_DELETE,"删除失败,请刷新后重试");
        }
        // 删除成功之后再删除图片
        uploadController.remove(img);
        return row;
    }

    @Override
    public int updateItem(ItemDetailVO itemDetailVO) {
        log.debug("开始处理更新物资信息的请求");
        itemDetailVO.setItemModified(LocalDateTime.now());
        log.debug("提交过来的物资信息为:{}",itemDetailVO);
        int row = mapper.updateItem(itemDetailVO);
        if (row != 1){
            throw new ServiceException(State.ERR_UPDATE,"更新失败,请刷新后重试");
        }
        return row;
    }

    @Override
    public ItemDetailVO getItemDetailsByName(String name) {
        ItemDetailVO item = mapper.getItemDetailsByName(name);
        if (item == null){
            throw new ServiceException(State.ERR_ITEM_NOT_FOUND,"查询失败,查询的物资名称不存在");
        }
        return item;
    }

    @Override
    public ItemDetailVO getItemDetailsById(Long id) {
        ItemDetailVO item = mapper.getItemDetailsById(id);
        if (item == null){
            throw new ServiceException(State.ERR_ITEM_NOT_FOUND,"查询失败,查询的物资名称不存在");
        }
        return item;
    }

    @Override
    public List<ItemSimpleVO> getListItems(String keyword) {
        return mapper.getListItems(keyword);
    }
}
